/**
 * 
 */
package com.dharma.mydiet.view;

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.dharma.mydiet.R;
import com.dharma.mydiet.storage.DAOFood;
import com.dharma.mydiet.storage.TransactionalFood;
import com.dharma.mydiet.utility.Utils;
import com.dharma.mydiet.utility.Utils.TextFieldType;
import com.google.analytics.tracking.android.EasyTracker;

/**
 * @author josh
 * 
 */
public class NewFoodActivity extends Activity {

    private Button save;
    private TextView textName;
    private TextView textCalories;
    private TextView textCarbohidrates;
    private TextView textProteins;
    private TextView textLipids;
    private TextView textSugar;
    private TextView textColesterol;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.fragment_alimento);
	int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
	Utils.applyCustomFont((TextView) findViewById(title), getApplicationContext());
	ActionBar ab = getActionBar();
	ab.setSubtitle("Cargar nuevo tipo de alimento");
	save = (Button) findViewById(R.id.buttonSave);
	if (save != null) {
	    save.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
		    if (isValidEntry()) {
			TransactionalFood food = new TransactionalFood(0, textName.getText().toString(), Integer
				.parseInt(textCalories.getText().toString()), Float.parseFloat(textCarbohidrates
				.getText().toString()), Float.parseFloat(textProteins.getText().toString()), Float
				.parseFloat(textLipids.getText().toString()), Integer.parseInt(textSugar.getText()
				.toString()), Float.parseFloat(textColesterol.getText().toString()));

			DAOFood.getInstance().insertFood(food);
			finish();
		    }
		}
	    });
	}
    }

    public boolean isValidEntry() {
	boolean isValid = true;

	textName = (TextView) findViewById(R.id.nombre);
	if (!Utils.isValidTextField(TextFieldType.NAME, textName.getText().toString())) {
	    textName.setError("Entrada Invalida.");
	    isValid = false;
	}
	textCalories = (TextView) findViewById(R.id.tcalorias);
	if (!Utils.isValidTextField(TextFieldType.NUMBER_INT, textCalories.getText().toString())) {
	    textCalories.setError("Entrada Invalida.");
	    isValid = false;
	}
	textCarbohidrates = (TextView) findViewById(R.id.carbohidratos);
	if (!Utils.isValidTextField(TextFieldType.NUMBER_INT, textCarbohidrates.getText().toString())) {
	    textCarbohidrates.setError("Entrada Invalida.");
	    isValid = false;
	}
	textProteins = (TextView) findViewById(R.id.proteinas);
	if (!Utils.isValidTextField(TextFieldType.NUMBER_INT, textProteins.getText().toString())) {
	    textProteins.setError("Entrada Invalida.");
	    isValid = false;
	}
	textLipids = (TextView) findViewById(R.id.lipidos);
	if (!Utils.isValidTextField(TextFieldType.NUMBER_INT, textLipids.getText().toString())) {
	    textLipids.setError("Entrada Invalida.");
	    isValid = false;
	}
	textSugar = (TextView) findViewById(R.id.azucar);
	if (!Utils.isValidTextField(TextFieldType.NUMBER_INT, textSugar.getText().toString())) {
	    textSugar.setError("Entrada Invalida.");
	    isValid = false;
	}
	textColesterol = (TextView) findViewById(R.id.colesterol);
	if (!Utils.isValidTextField(TextFieldType.NUMBER_INT, textColesterol.getText().toString())) {
	    textColesterol.setError("Entrada Invalida.");
	    isValid = false;
	}
	return isValid;
    }

    @Override
    public void onStart() {
	super.onStart();
	EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
	super.onStop();
	EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}
